package com.lcq.privacy;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Interpolator;
import android.widget.ImageView;

import com.lcq.privacysupport.PrivacyCallback;
import com.lcq.privacysupport.Privacy;

public class LaunchActivity extends Activity {
    protected ImageView img;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!isTaskRoot()) {//防止应用切换后台后，启动页再次被拉起
            final Intent intent = getIntent();
            final String intentAction = intent.getAction();
            if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
                finish();
                return;
            }
        }

        //设置全屏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_launch);
        img = findViewById(R.id.iv);
        img.setAlpha(0f);
        img.setBackgroundColor(Color.WHITE);

        startAnim();
    }

    /**
     * 开始3秒的启动页动画
     */
    public void startAnim() {
        ValueAnimator va = ValueAnimator.ofFloat(0, 0.3f, 0.8f, 1f);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float params = (float) animation.getAnimatedValue();
                img.setAlpha(params);
            }
        });
        va.setInterpolator(new Interpolator() {
            @Override
            public float getInterpolation(float input) {
                return input;
            }
        });
        va.setDuration(3000);
        va.setRepeatCount(0);
        va.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                //动画结束后进行隐私协议检查，收到onAgree回调后才启动主界面，并finish自身
                new Privacy(LaunchActivity.this).check(new PrivacyCallback() {
                    @Override
                    public void onAgree() {
                        startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                        finish();
                    }
                });
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        va.start();
    }

    @Override
    public void onBackPressed() {
        //启动页返回键不finish
    }
}
